package com.example.japinhands.ui.screen

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import com.example.japinhands.JapViewModel
import com.example.japinhands.data.Sentence
import com.example.japinhands.data.WordConstant
import com.example.japinhands.data.WordUI
import kotlin.concurrent.thread

@OptIn(ExperimentalLayoutApi::class, ExperimentalMaterial3Api::class)
@Composable
fun TestNewSentenceScreen(
    viewModel: JapViewModel,
    modifier: Modifier = Modifier
) {
    val tag = "TestNewSentenceScreen"
    val wordList = remember {
        mutableStateListOf<WordUI>().apply {
            val words = listOf(
                WordUI("雨", false),
                WordUI("自転車", false),
                WordUI("来る", false),
                WordUI("勉強する", false),
                WordUI("ありがとう", false),
                WordUI("父", false)
            )
            addAll(words)
        }
    }

    var sentenceText by remember {
        mutableStateOf("")
    }

    Column {
        FlowRow {
            wordList.forEachIndexed { index, word ->
                Row {
                    Checkbox(
                        checked = word.isChecked,
                        onCheckedChange = { check ->
//                            wordList.toMutableList().apply {
//                                find {
//                                    it.name == word.name
//                                }?.isChecked = check
//                            }
                            // 注意copy函数的原理，为什么可以这么写
                            wordList[index] = wordList[index].copy(isChecked = check)
                            Log.d(tag, "Word ${word.name} isChecked: ${word.isChecked}")
                        }
                    )
                    Text(text = word.name)
                }
            }
        }

        TextField(
            modifier = Modifier.fillMaxWidth(),
            value = sentenceText,
            onValueChange = { sentenceText = it },
            label = { Text(text = "New Sentence") }
        )

        Button(onClick = {
//            sentenceText = wordList.filter {
//                it.isChecked
//            }.toString()
            val key = WordConstant.newSentenceKey
            val s = Sentence(
                content = sentenceText
            )
            thread {
                val id = viewModel.insertSentence(s)
                Log.d(tag, "After sentence insertion, id: $id, key: $key")
            }
        }) {
            Text(text = "Submit")
        }
    }

}